package com.softserveinc.moviedbapp.appwidget;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.softserveinc.moviedbapp.application.MovieDBApplication;
import com.softserveinc.moviedbapp.manager.DataManager;
import com.softserveinc.moviedbapp.model.Movie;
import com.softserveinc.moviedbapp.util.MovieAppConstants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class UpdateService extends Service {
	public static final String INTENT_UPDATE_YOURSELF = "update.yourself.you.focker";

	private static final String TAG = UpdateService.class.getSimpleName();

	public static final String UPDATE = "widget_update";
	public static final String UPDATE_CONFIG_CHANGED = "widget_update_config_changed";
	public static final String CONFIGURE = "widget_configure";
	public static final String SHOW_MOVIE_DETAILS = "widget_movie_details";
	public static final String UPDATE_ITSELF = "widget_update_itself";

	private SharedPreferences prefs;
	private DataManager dataManager;
	private ExecutorService executorService;
	private Map<Integer, Integer> currentPositionMap;

	@Override
	public void onCreate() {
		Log.d(TAG, "in onCreate() ");
		prefs = getSharedPreferences("widget_prefs", 0);
		dataManager = ((MovieDBApplication) getApplication()).getDataManager();
		executorService = Executors.newSingleThreadExecutor();
		currentPositionMap = new HashMap<Integer, Integer>();
	}

	@Override
	public int onStartCommand(final Intent intent, final int flags, final int startId) {
		Log.d(TAG, "in onStartCommand() ");
		final String action = intent.getAction();
		if (action != null) {
			final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
			Log.d(TAG, "in onStartCommand() and action is " + action);
			Log.d(TAG, "in onStartCommand() and widget id is " + appWidgetId);

			executorService.execute(new Runnable() {
				@Override
				public void run() {
					/* if the user pressed configure button, launch config activity */
					if (action.equals(CONFIGURE)) {
						startActivity(intent);
						return;

					} else if (action.equals(UPDATE_CONFIG_CHANGED)) {
						Log.d(TAG, "in onStartCommand() updating configuration");
						String orderBy = prefs.getString("orderBy" + appWidgetId, "release");
						String results = prefs.getString("results" + appWidgetId, "30");
						dataManager.manageMovieListForWidget(orderBy, results);
					}

					/* ordering preference and max results */
					String orderBy = prefs.getString("orderBy" + appWidgetId, "release");
					String results = prefs.getString("results" + appWidgetId, "30");

					Integer currentPosition = currentPositionMap.get(appWidgetId);
					if (currentPosition == null || currentPosition == Integer.parseInt(results) - 1) {
						currentPosition = 0;
					} else {
						currentPosition++;
					}
					currentPositionMap.put(appWidgetId, currentPosition);

					Log.d(TAG, "in onStartCommand() gonna get movie for position " + currentPosition + " and order "
							+ orderBy);
					List<Movie> movies = dataManager.manageMovieForWidget(orderBy, currentPosition);

					Intent intentForceUpdate = new Intent(INTENT_UPDATE_YOURSELF);
					intentForceUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
					Bundle movieBundle = new Bundle();
					int pos = 0;
					for (Movie movie : movies) {
						movieBundle.putParcelable(MovieAppConstants.MOVIE_OBJECT + pos, movie);
						pos++;
					}
					intentForceUpdate.putExtra(MovieAppConstants.MOVIE_ITEMS, movieBundle);
					Log.d(TAG, "in onStartCommand() got movie, sending broadcast");
					sendBroadcast(intentForceUpdate);
				}
			});
		}
		return Service.START_NOT_STICKY;
	}

	@Override
	public IBinder onBind(final Intent intent) {
		return null;
	}

}